Expand description
Trait and macros to represent Rust errors in JavaScript.
§The JsError
macro
Macro to define the JsErrorClass
trait on a struct or enum.
The macro does not provide functionality to related to the get_message
function, as one can combine the thiserror
well with this macro.
§Attributes
§#[class]
This attribute accepts 3 possible kinds of value:
GENERIC
,TYPE
, and a few more that are defined in thebuiltin_classes
module, without the_ERROR
suffix.- A text value ie
"NotFound"
. If a text value is passed that is a valid builtin (see the previous point), it will error out as the special identifiers are preferred to avoid mistakes. inherit
: this will inherit the class from whatever field is marked with the#[inherit]
attribute. Alternatively, the#[inherit]
attribute can be omitted if only one field is present in the enum variant or struct. This value is inferred if the class attribute is missing and only a single field is present on a struct, however for enums this inferring is not done.
§#[property]
This attribute allows defining fields as additional properties that should be defined on the JavaScript error.
The type of the field needs to implement a .to_string()
function for it
being able to be inherited.
§#[inherit]
This attribute allows defining a field that should be used to inherit the class and properties.
This is inferred if only one field is present in the enum variant or struct.
The class is only inherited if the class
attribute is set to inherit
.
§Examples
§Basic usage
#[derive(Debug, thiserror::Error, deno_error::JsError)]
pub enum SomeError {
#[class(generic)]
#[error("Failure")]
Failure,
#[class(inherit)]
#[error(transparent)]
Io(#[inherit] std::io::Error),
}
§Top-level class
#[derive(Debug, thiserror::Error, deno_error::JsError)]
#[class(generic)]
pub enum SomeError {
#[error("Failure")]
Failure,
#[class(inherit)] // overwrite the top-level
#[error(transparent)]
Io(#[inherit] std::io::Error),
}
§Defining properties
#[derive(Debug, thiserror::Error, deno_error::JsError)]
#[class(generic)]
pub enum SomeError {
#[class(not_supported)]
#[error("Failure")]
Failure {
#[property]
code: u32,
},
#[error("Warning")]
Warning(#[property = "code"] u32),
#[class(inherit)] // inherit properties from `std::io::Error`
#[error(transparent)]
Io(#[inherit] std::io::Error),
}
§Inferred inheritance
#[derive(Debug, thiserror::Error, deno_error::JsError)]
#[error("My io error")]
pub struct SomeError(std::io::Error);
#[derive(Debug, thiserror::Error, deno_error::JsError)]
#[class(inherit)]
#[error("My io error")]
pub struct SomeError(std::io::Error);
#[derive(Debug, thiserror::Error, deno_error::JsError)]
#[class(generic)] // don't inherit the error
#[error("My io error")]
pub struct SomeError(std::io::Error);
#[derive(Debug, thiserror::Error, deno_error::JsError)]
#[class(type)]
pub enum SomeError {
#[error("Failure")]
Failure,
#[class(inherit)]
#[error(transparent)]
Io(std::io::Error),
}
Modules§
- Various built-in error classes, mainly related to the JavaScript specification. May include some error classes that are non-standard.
Macros§
- Macro which lets you wrap an existing error in a new error that implements the
JsErrorClass
trait. This macro however does currently not support the special identifiers that theJsError
macro supports.
Traits§
- Trait to implement how an error should be represented in JavaScript.
Functions§
- Get the error code for a provided
std::io::Error
.